/********************************************************************************
* File Name:	welcomeStu.cpp
* Description:	第10章实例训练          
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1  
* E_mail: openlinux2011@gmail.com		                
*
********************************************************************************/
#include "welcomeStu.h"
#include <QSqlRecord>
#include <QMessageBox>
#include <QModelIndex>
#include <QString>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>

extern QString snumber ; //记录登录学生的用户名

WelcomeStu::WelcomeStu(QWidget* parent) : QDialog(parent)
{
    setupUi(this);

    /*信号与槽函数连接*/
    connect(stuAddListUi.pbStuAddListBack,SIGNAL(clicked()),this,SLOT(backStu())); //返回学生登录主界面
    connect(this->pbStuEnter , SIGNAL(clicked()),this , SLOT(editStu())); //修改学生信息
    connect(this->sure , SIGNAL(clicked()),this , SLOT(editStuPwd())); //修改登录密码
}

void WelcomeStu::newStuAddList()  //显示点菜界面，隐藏学生登录主界面
{
    stuAddListUi.show();
    stuAddListUi.StuAddList_load();
    this->hide();
}

void WelcomeStu::backStu() //显示学生登录主界面
{
    this->show();
}

void WelcomeStu::editStu() //更改学生信息
{
    QSqlQuery query;
    QString str = "update Student set sname='"+this->leStuName->text()+"',sclass='"+this->leStuClass->text()+"',sdom='"+this->leStuDom->text()+"',sphonenumber='"+this->leStuPhoneNumber->text()+"',ssex='"+this->leStuSex->text()+"' where snumber = '"+snumber+"'";
    query.exec(str);
    QMessageBox::warning(this , tr("数据更新") , tr("数据库已经成功更新"));
}

void WelcomeStu::editStuPwd() //更改密码信息
{
    QSqlQuery query;
    QString str;

    /*判断输入是否合法*/
    if(this->leStuOldKey->text().isEmpty() || this->leStuNewKey_2->text().isEmpty() || this->LeStuNewKey_1->text().isEmpty())
    {
        QMessageBox::warning(this , tr("输入数据不完整"),tr("请重新输入"));

    }
    else if ( this->leStuNewKey_2->text() != this->LeStuNewKey_1->text()) //判断输入密码是否正确
    {
        QMessageBox::warning(this , tr("输入新密码不匹配"),tr("请重新输入"));       
    }
    else //更新密码
    {
        str = "select spasswd from Student where snumber = '"+snumber+"'";
        query.exec(str);
        query.first();
        if(query.value(0).toString() == this->leStuOldKey->text())
        {
            str = "update Student set spasswd = '"+this->leStuNewKey_2->text()+"'where snumber ='"+snumber+"'";
            if(query.exec(str))
                QMessageBox::warning(this , tr("更新密码"),tr("您的密码已经更新"));

        }
        else
        {
            QMessageBox::warning(this , tr("更新错误"),tr("用户名或密码错误"));
        }               
    }

    /*清空输入信息*/
    this->leStuOldKey->clear();
    this->leStuNewKey_2->clear();
    this->LeStuNewKey_1->clear();
}

void WelcomeStu::WelcomeStu_load() //学生登录界面加载
{
    QSqlQuery query;
    QString str = "select * from Student where snumber = '" +snumber +"'";
    query.exec(str);
    if(query.next())
    {
     this->leStuCardNumber->setText(query.value(5).toString());
     this->leStuClass->setText(query.value(2).toString());
     this->leStuDom->setText(query.value(3).toString());
     this->leStuName->setText(query.value(0).toString());
     this->leStuNumber->setText(query.value(1).toString());
     this->leStuPhoneNumber->setText(query.value(4).toString());
     this->leStuSex->setText(query.value(8).toString());
     QString temp = query.value(5).toString();
     str = "select * from card where scardnumber ='"+temp+"'";
     query.exec(str);
     query.first();
     this->leStuMoney->setText(query.value(1).toString());
    }
    else
        QMessageBox::warning(this ,"错误","数据读取失败");
}
